<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称"> {{ spu.spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" placeholder="规格描述" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <!-- 两个下拉框 -->
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="(attr, index) in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="(attrValue, index) in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item label="颜色" v-for="(saleAttr, index) in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="(saleAttrValue, index) in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <!-- 底部表格 -->
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="80"> </el-table-column>
          <el-table-column align="center" prop="spuName" label="图片" width="width">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="imgName" label="名称" width="width"> </el-table-column>
          <el-table-column align="center" prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button type="primary" v-if="row.isDefault == 0" @click="changeDefault(row)">设置默认</el-button>
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      spuImageList: [], //存储图片信息
      spuSaleAttrList: [], //存储销售属性
      attrInfoList: [], // 存储平台属性
      //收集数据
      skuInfo: {
        //第一类父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类绑定的数据
        price: 0,
        skuName: '',
        skuDesc: '',
        //第三类收集的数据
        //平台属性
        skuAttrValueList: [
          {
            attrId: 0,
            valueId: 0,
          },
        ],
        skuDefaultImg: '', //默认图片

        skuImageList: [
          {
            // id: 0,
            // imgName: 'string',
            // imgUrl: 'string',
            // isDefault: 'string',
            // skuId: 0,
            // spuImgId: 0,
          },
        ],
        //销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: 'string',
          //   saleAttrValueId: 0,
          //   saleAttrValueName: 'string',
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
      spu: {},
      //复选框 收集的图片 收集的数据不符合服务器模板 缺少isDefault
      imageList: [],
    }
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      // 收集父组件给的数据
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu
      //获取图片
      let result = await this.$API.spu.reqSpuImageLIst(spu.id)
      // console.log(result)
      if (result.code == 200) {
        let list = result.data
        list.forEach((item) => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }
      // 获取销售属性
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      // console.log(result1)
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data
      }
      // 获取平台属性
      let result2 = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      // console.log(result2)
      if (result2.code == 200) {
        this.attrInfoList = result2.data
      }
    },
    //table 复选框变化回调
    handleSelectionChange(params) {
      // console.log(params)//得到的是复选框选择的信息
      this.imageList = params
    },
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0
      })
      row.isDefault = 1 //点击的显示默认
      //收集到默认图片的信息
      this.skuInfo.skuDefaultImg = row
    },
    //取消按钮
    cancle() {
      this.$emit('changeScenes', 0)
      Object.assign(this._data, this.$options.data())
    },
    //保存按钮
    async save() {
      // alert(12435)
      // 收集数据先整理
      //平台属性
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this

      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({ attrId, valueId })
        }
        return prev
      }, [])
      //平台属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      //图片收集
      // console.log(imageList);
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        }
      })
      //发请求
      let result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code == 200) {
        this.$emit('changeScenes', 0)
        this.$message({ type: 'success', message: '添加成功' })
      }
    },
  },
}
</script>

<style></style>
